$(function(){
	wxnews_ctrl();
	
	
})
	//微信控制

function wxnews_click() {
	var msg = sessionStorage.getItem("wxnewsdata");
	$('.wxnews').each(function(index, element) {
		$(this).removeClass('select_news');
	});

	$(this).addClass('select_news');

	var new_id = parseInt($('.select_news').attr('news_id'));
	var session = new Object();
	if (msg != null&&msg != '') {
		session = JSON.parse(msg);
	}

	var session_empty = true;
	for (var n in session) {

		session_empty = false;
	}

	if (typeof(session[new_id]) == 'undefined' || session[new_id] == '') {
		$('#type2 input[name=news_title]').val('');
		$('#type2 input[name=news_id]').val('');
		$('#type2 input[name=news_des]').val('');
		$('#type2 input[name=news_url]').val('');
		$('#type2 input[name=thumb_url]').val('');
	}
	if (!session_empty && typeof(session[new_id]) != 'undefined') {

		if (!session[new_id]) {
			session[new_id].news_id = session[new_id].news_title = session[new_id].news_des = session[new_id].news_url = session[new_id].thumb_url = '';
		} else {
			$('#type2 input[name=news_title]').val(session[new_id].news_title);
			$('#type2 input[name=news_id]').val(session[new_id].news_id);
			$('#type2 input[name=news_des]').val(session[new_id].news_des);
			$('#type2 input[name=news_url]').val(session[new_id].news_url);
			$('#type2 input[name=thumb_url]').val(session[new_id].thumb_url);
		}
	}
}

function set_session() {
	var new_id = $('.select_news').attr('news_id');
	var msg = sessionStorage.getItem("wxnewsdata");

	var session = new Object();
	var data = new Object();
	data.news_title = $('#type2 input[name=news_title]').val();
	data.news_id = $('#type2 input[name=news_id]').val();
	data.news_des = $('#type2 input[name=news_des]').val();
	data.news_url = $('#type2 input[name=news_url]').val();
	data.thumb_url = $('#type2 input[name=thumb_url]').val();
	if (msg != null&&msg != '') {
		session = JSON.parse(msg);
		session[new_id] = data;
		
	} else {
		session[new_id] = data;
	}

	sessionStorage.setItem("wxnewsdata", JSON.stringify(session));


}

function wxnews_ctrl() {
		var msg = sessionStorage.getItem("wxnewsdata");
		var session = new Object();
		
		if (msg != null&&msg != '') {
			session=JSON.parse(msg);

		}

		if (typeof(session[1]) != 'undefined') {
			$('#type2 input[name=news_title]').val(session[1].news_title);
			$('#type2 input[name=news_id]').val(session[1].news_id);

			$('#type2 input[name=news_des]').val(session[1].news_des);
			$('#type2 input[name=news_url]').val(session[1].news_url);
			$('#type2 input[name=thumb_url]').val(session[1].thumb_url);
		}

		$('.wxnews').bind('click', wxnews_click);
		$('.cut_wxnews').click(function() {
			var news_num = $('.wxnews').length;
			var msg = sessionStorage.getItem("wxnewsdata");
			var session = JSON.parse(msg);

			if (news_num > 1) {
				session[news_num] = '';
				sessionStorage.setItem("wxnewsdata", JSON.stringify(session));
				$(this).prev().prev().remove();
			}

		});
		$('.add_wxnews').click(function() {
			var wxnews_num = $('.wxnews').length + 1;
			if (wxnews_num == 11) {
				alert('图文数不能超过十');
				return;
			}
			var html = '<a news_id=' + wxnews_num + ' class="wxnews" href="javascript:void(0)">图文' + wxnews_num + '</a>';
			$(this).before(html);
			$('.wxnews').bind('click', wxnews_click);
		});
		$('#type2 input').bind('change', set_session);

	}
	//添加主菜单

function add_main_menu() {
		$.post('index.php?g=admin&m=wxmenu&a=add_main_menu', {}, function(result) {
			//alert(result);
			if (result.error == 0) {

				if (parent.$('.msg_detail')) {
					parent.$('.msg_detail').remove();
				}
				parent.$(" body").append("<div class=\'msg_detail\'><div class=\'msg_title\'><a class=\'msg_close\' href=\'javascript:void(0)\'>X</a></div><div class=\'msg_main\'><ul><li><div class=\'f_l\'>父分类：</div><p><select name=\'parent\'><option value=\'0\'>请选择父菜单</option>" + result.cat + "</select></p></li><li><div class=\'f_l\'>名称：</div><p><input type=\'text\' name=\'name\' value=\'\'></p></li><li><div class=\'f_l\'>类型：</div><p><input class=\'tabBtn\' data-name=\'tab\' data-value=\'1\' checked=\'checked\'  type=\'radio\' name=\'type\' value=\'1\'>链接<input class=\'tabBtn\' data-name=\'tab\' data-value=\'2\'  type=\'radio\' name=\'type\' value=\'2\'>关联信息</p></li><li id=\'tab1\'><div class=\'f_l\'>链接：</div><p><input type=\'text\' name='\link_url'\ value=\'http://\'></p></li><li id=\'tab2\' style=\'display:none\'><div class=\'f_l\'>规则：</div><p><select name='rule'>" + result.data + "</select></p></li><li><a href='javascript:void(0)' class='litte_btn f_r'>提交</a><input name=\'cat_id\' type=\'hidden\' value=\'\'></li></ul><div style=\'clear:both; height:15px;\'></div></div></div>");
				parent.$('.mask').show();
				parent.$('.msg_detail .litte_btn').click(main_menu_submit);
				parent.$('.msg_close').click(function() {
					parent.$('.msg_detail').remove();
						parent.$('.mask').hide();
				});
			} else {
				alert(result.content);

			}
		}, 'json');
	}
	//编辑菜单

function edit_menu(id) {

		$.post('index.php?g=admin&m=wxmenu&a=edit_menu', {
			id: id
		}, function(result) {
			//alert(result);
			if (result.error == 0) {
				var data = result.data;
				var url = typeof(data.url) == 'undefined' ? '' : data.url;
				if (data.type == '1') {
					var check1 = "checked='checked'";
					var check2 = "";


				} else {
					var check1 = "";
					var check2 = "checked='checked'";

				}


				if (parent.$('.msg_detail')) {
					parent.$('.msg_detail').remove();
				}
				parent.$(" body").append("<div class=\'msg_detail\'><div class=\'msg_title\'><a class=\'msg_close\' href=\'javascript:void(0)\'>X</a></div><div class=\'msg_main\'><ul><li><div class=\'f_l\'>父分类：</div><p><select name=\'parent\'>" + result.cat + "</select></p></li><li><div class=\'f_l\'>名称：</div><p><input type=\'text\' name=\'name\' value=\'" + data.name + "\'></p></li><li><div class=\'f_l\'>类型：</div><p><input class=\'tabBtn\' data-name=\'tab\' data-value=\'1\' " + check1 + "  type=\'radio\' name=\'type\' value=\'1\'>链接<input class=\'tabBtn\' data-name=\'tab\' data-value=\'2\' " + check2 + "  type=\'radio\' name=\'type\' value=\'2\'>关联信息</p></li><li id=\'tab1\'><div class=\'f_l\'>链接：</div><p><input type=\'text\' name='\link_url'\ \'" + url + "\'></p></li><li id=\'tab2\' style=\'display:none\'><div class=\'f_l\'>规则：</div><p><select name='rule'>" + result.list + "</select></p></li><li><a href='javascript:void(0)' class='litte_btn f_r'>提交</a><input name=\'cat_id\' type=\'hidden\' value=\'" + id + "\'><input name=\'old_pid\' type=\'hidden\' value=\'" + data.parent_id + "\'></li></ul><div style=\'clear:both; height:15px;\'></div></div></div>");
				parent.$('.mask').show();
				parent.$('.msg_detail .litte_btn').click(menu_update);
				parent.$('.msg_close').click(function() {
					parent.$('.msg_detail').remove();
						parent.$('.mask').hide();
				});
				
				if (data.type == '1') {

					parent.$('#tab2').css('display', 'none');
					parent.$('#tab1').css('display', 'block');

				} else {

					parent.$('#tab1').css('display', 'none');
					parent.$('#tab2').css('display', 'block');
				}
				parent.$('.msg_close').click(function() {
					parent.$('.msg_detail').remove();
					parent.$('.mask').hide();
				});
			} else {
				alert(result.content);

			}
		}, 'json');
	}


function main_menu_submit() {
	
	var menu = new Object;
	menu.name = parent.$('.msg_main input[name=name]').val();
	menu.parent_id = parent.$('.msg_main select[name=parent]').val();
	menu.type = parent.$('.msg_main input[name=type]:checked').val();
	menu.rule = parent.$('.msg_main select[name=rule]').val();
	menu.url = parent.$('.msg_main input[name=link_url]').val();

$.ajax({'url':'index.php?g=admin&m=wxmenu&a=new_main_menu',
'data':{menu:menu},
'type':'post',
'dataType':'json',
'contentType': "application/x-www-form-urlencoded; charset=utf-8", 
'success':function(result) {
		if (result.error > 0) {
			alert(result.content);
		} else {
			alert(result.content);
			parent.$('.msg_detail').remove();
		parent.$('.mask').hide();
					
		//	window.history.go(0);
		}
		
	}
});
	
}

function menu_update() {
		var menu = new Object;
		menu.id = parent.$('.msg_main input[name=cat_id]').val();
		menu.name = parent.$('.msg_main input[name=name]').val();
		menu.parent_id = parent.$('.msg_main select[name=parent]').val();
		menu.old_pid= parent.$('.msg_main input[name=old_pid]').val();
		menu.type = parent.$('.msg_main input[name=type]:checked').val();
		menu.rule = parent.$('.msg_main select[name=rule]').val();
		menu.url = parent.$('.msg_main input[name=link_url]').val();

		$.post('index.php?g=admin&m=wxmenu&a=update_menu', {
			menu: menu
		}, function(result) {
			//alert(result);
			if (result.error > 0) {
				alert(result.content);
			} else {
				alert(result.content);
				parent.$('.msg_detail').remove();
						parent.$('.mask').hide();
			}
		}, 'json');
	}
function creareLink(id)
{
	
	var url ='http://'+window.location.host+'/index.php?g=home&m=article&a=view&id='+id
	$('input[name=news_url]').val(url);
	set_session();
}